using UnityEngine;
using System.Collections;

/// \class  gkAbyssalGiantSubmarineInvincibilityPhase
/// \brief  Mak the submarine invincible after losing a life
[AddComponentMenu("GK/AbyssalGiant/Submarine/gkAbyssalGiantSubmarineInvincibilityPhase")]
public class gkAbyssalGiantSubmarineInvincibilityPhase : MonoBehaviour 
{
	/// The event component
	private gkEventComponent m_rEventComponent;
	
	/// The life component
	private gkLifeComponent m_rLifeComponent;
	
	
	/// \brief  Called just after the game begin to play
	void Start()
	{
		// Grab the event component
		m_rEventComponent = GetComponent<gkEventComponent>();
		
		// Grab the life component
		m_rLifeComponent = GetComponent<gkLifeComponent>();
	}
	
	/// \brief  Called when the component is enabled
	void OnEnable() 
	{
		// Listen the shock event of the owner
		GetComponent<gkEventComponent>().AddEventHandler(gkLifeEvent.LifeLoss, HandleLifeLossEvent);
	}
	
	/// \brief  Called when the component is disabled
	void OnDisable() 
	{	
		// Remove the shock event of the owner
		GetComponent<gkEventComponent>().RemoveEventHandler(gkLifeEvent.LifeLoss, HandleLifeLossEvent);
	}
	
	/// \brief  Handle the life loss event
	void HandleLifeLossEvent(Object m_rEvent)
	{
		BeginInvincibilityPhase();
	}
	
	/// \brief  Stop the invincibility phase
	public void BeginInvincibilityPhase()
	{
		m_rLifeComponent.SetInvincibility(true);
		m_rEventComponent.RaiseEvent(gkAbyssalGiantInvincibiliyEvent.InvincibilityBegin);
	}
	
	/// \brief  End the invincibility phase
	public void EndInvincibilityPhase()
	{
		m_rLifeComponent.SetInvincibility(false);
		m_rEventComponent.RaiseEvent(gkAbyssalGiantInvincibiliyEvent.InvincibilityEnd);
	}
		
}